<?php

/**
 * ArticleCategorys actions.
 *
 * @package    cms
 * @subpackage ArticleCategorys
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ArticleCategorysActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
     $this->article_categorys = Doctrine::getTable('ArticleCategorys')
                                        ->createQuery()
                                        ->where('parent_id = ?',0)
                                        ->orderBy('sort_num DESC,created_at DESC')
                                        ->execute();
     $category = '';
     if(  $request->getParameter('id') > 0 )
     {
        $category = Doctrine::getTable('ArticleCategorys')->findOneById($request->getParameter('id'));
     }
     $this->form = new ArticleCategorysForm($category);
     if($request->isMethod('POST') || $request->isMethod('PUT'))
     {
         $this->processForm($request, $this->form);
     }
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if($form->isValid())
    {
      $form->save();
      $this->getUser()->setFlash('notice', '分类信息保存成功！');
      $this->redirect($this->generateUrl('',array('module'=>'ArticleCategorys')));
    }
  }
  
  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($article_category = Doctrine::getTable('ArticleCategorys')->findOneById(array($request->getParameter('id'))),
                                                                                                          sprintf('Object article_categorys does not exist (%s).', $request->getParameter('id')));
    $child_categorys_count = Doctrine::getTable('ArticleCategorys')->createQuery()->where('parent_id = ? ',$article_category->getId())->count();

    if($child_categorys_count > 0)
    {
        $this->getUser()->setFlash('error', '分类删除失败！请先删除该分类下的子分类');
    }else{
        $article_category->delete();
        $this->getUser()->setFlash('notice', '分类删除成功!');
    }
    
    $this->redirect($this->generateUrl('',array('module'=>'ArticleCategorys')));
  }

//  public function executeUpdate(sfWebRequest $request)
//  {
//    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
//    $this->forward404Unless($article_categorys = Doctrine::getTable('ArticleCategorys')->find(array($request->getParameter('id'))), sprintf('Object article_categorys does not exist (%s).', $request->getParameter('id')));
//    $this->form = new ArticleCategorysForm($article_categorys);
//
//    $this->processForm($request, $this->form);
//
//    $this->redirect('ArticleCategorys/index');
//  }
//
//  public function executeShow(sfWebRequest $request)
//  {
//    $this->article_categorys = Doctrine::getTable('ArticleCategorys')->find(array($request->getParameter('id')));
//    $this->forward404Unless($this->article_categorys);
//  }
//
//  public function executeNew(sfWebRequest $request)
//  {
//    $this->form = new ArticleCategorysForm();
//  }
//
//
//  public function executeEdit(sfWebRequest $request)
//  {
//    $this->forward404Unless($article_categorys = Doctrine::getTable('ArticleCategorys')->find(array($request->getParameter('id'))), sprintf('Object article_categorys does not exist (%s).', $request->getParameter('id')));
//    $this->form = new ArticleCategorysForm($article_categorys);
//  }
//
//  public function executeUpdate(sfWebRequest $request)
//  {
//    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
//    $this->forward404Unless($article_categorys = Doctrine::getTable('ArticleCategorys')->find(array($request->getParameter('id'))), sprintf('Object article_categorys does not exist (%s).', $request->getParameter('id')));
//    $this->form = new ArticleCategorysForm($article_categorys);
//
//    $this->processForm($request, $this->form);
//
//    $this->setTemplate('edit');
//  }
//

//

}
